//
// Created by 32827 on 2024/1/17.
//
#include "yzf_i2c.h"

YZF_RESULT yzf_i2c_open(YZF_I2C_POINTER i2c){
    switch (i2c) {
        case HR_I2C_BASE_ADDR:
            YZF_REGV32 HR_CLK_GATE_EN YZF_ADD (uint32_t) YZF_BIT(0);
            break;
        default:
            return YZF_NOT_FOUND;
    }
    return YZF_OK;
}
uint32_t lastFreq;
YZF_RESULT yzf_i2c_source_clock_freq(YZF_I2C_POINTER i2c,uint32_t freq){
    if (freq < 100*1000)
	{
		freq = 100*1000;
	}		
	else if (freq > 400*1000)
	{
		freq = 400*1000;
	}
    //Get clk
    clk_div_reg clk_div;

	clk_div.w = YZF_REGV32 HR_CLK_DIV_CTL;
	uint32_t apbclk  = (W800_PLL_CLK_MHZ/(clk_div.b.CPU)) / clk_div.b.BUS2;
    uint32_t div = (apbclk * 1000000)/(5 * freq) - 1;
    switch (i2c) {
        case HR_I2C_BASE_ADDR:
            YZF_REGV32 HR_I2C_PRER_LO YZF_SET div & 0xff;
	        YZF_REGV32 HR_I2C_PRER_HI YZF_SET (div>>8) & 0xff;
            break;
        default:
            return YZF_NOT_FOUND;
    }
    lastFreq=freq;
    return YZF_OK;
}
YZF_RESULT yzf_i2c_start(YZF_I2C_POINTER i2c){
    switch (i2c) {
        case HR_I2C_BASE_ADDR:
            YZF_REGV8 HR_I2C_CR_SR YZF_SET YZF_BIT(7);
            break;
        default:
            return YZF_NOT_FOUND;
    }
    return YZF_OK;
}
YZF_RESULT yzf_i2c_write_data(YZF_I2C_POINTER i2c,YZF_I2C_DATA data){
    switch (i2c) {
        case HR_I2C_BASE_ADDR:
            YZF_REGV8 HR_I2C_TX_RX YZF_SET data;
            YZF_REGV8 HR_I2C_CR_SR YZF_SET YZF_BIT(4);
            break;
        default:
            return YZF_NOT_FOUND;
    }
    return YZF_OK;
}
YZF_I2C_DATA yzf_i2c_read_data(YZF_I2C_POINTER i2c){
    switch (i2c) {
        case HR_I2C_BASE_ADDR:
            return YZF_REGV8 HR_I2C_TX_RX;
        default:
            return 0;
    }
}
YZF_RESULT yzf_i2c_set_address_10(YZF_I2C_POINTER i2c,uint16_t address){
    return YZF_OK;
}
YZF_RESULT yzf_i2c_set_address_7(YZF_I2C_POINTER i2c,uint8_t address){
    return YZF_OK;
}
YZF_RESULT yzf_i2c_soft_reset(YZF_I2C_POINTER i2c){
    switch (i2c) {
        case HR_I2C_BASE_ADDR:
            YZF_REGV8 HR_I2C_CR_SR YZF_SET YZF_BIT(0);
            break;
        default:
            return YZF_NOT_FOUND;
    }
    return YZF_OK;
}
YZF_RESULT yzf_i2c_soft_reset_clear(YZF_I2C_POINTER i2c){
    switch (i2c) {
        case HR_I2C_BASE_ADDR:
            YZF_REGV8 HR_I2C_CR_SR YZF_SET YZF_BIT(0);
            break;
        default:
            return YZF_NOT_FOUND;
    }
    return YZF_OK;
}
YZF_RESULT yzf_i2c_ack_enable(YZF_I2C_POINTER i2c){
    switch (i2c) {
        case HR_I2C_BASE_ADDR:
            YZF_REGV8 HR_I2C_CR_SR YZF_SET 0x0;
            break;
        default:
            return YZF_NOT_FOUND;
    }
    return YZF_OK;
}
YZF_RESULT yzf_i2c_ack_disable(YZF_I2C_POINTER i2c){
    switch (i2c) {
        case HR_I2C_BASE_ADDR:
            YZF_REGV8 HR_I2C_CR_SR YZF_SET YZF_BIT(3);
            break;
        default:
            return YZF_NOT_FOUND;
    }
    return YZF_OK;
}
YZF_RESULT yzf_i2c_stop(YZF_I2C_POINTER i2c){
        switch (i2c) {
        case HR_I2C_BASE_ADDR:
            YZF_REGV8 HR_I2C_CR_SR YZF_SET YZF_BIT(6);
            break;
        default:
            return YZF_NOT_FOUND;
    }
    return YZF_OK;
}
YZF_RESULT yzf_i2c_disable_clock_stretching(YZF_I2C_POINTER i2c){
    return YZF_ERROR;
}
YZF_RESULT yzf_i2c_call_enable(YZF_I2C_POINTER i2c){
    return YZF_ERROR;
}
YZF_RESULT yzf_i2c_call_disable(YZF_I2C_POINTER i2c){
    return YZF_ERROR;
}
YZF_RESULT yzf_i2c_pec_enable(YZF_I2C_POINTER i2c){
    return YZF_ERROR;
}
YZF_RESULT yzf_i2c_pec_disable(YZF_I2C_POINTER i2c){
    return YZF_ERROR;
}
YZF_RESULT yzf_i2c_apr_enable(YZF_I2C_POINTER i2c){
    return YZF_ERROR;
}
YZF_RESULT yzf_i2c_apr_disable(YZF_I2C_POINTER i2c){
    return YZF_ERROR;
}
YZF_RESULT yzf_i2c_enable(YZF_I2C_POINTER i2c){
    switch (i2c) {
        case HR_I2C_BASE_ADDR:
           YZF_REGV32 HR_I2C_CTRL YZF_ADD I2C_CTRL_ENABLE;
            break;
        default:
            return YZF_NOT_FOUND;
    }
    return YZF_OK;
}
YZF_RESULT yzf_i2c_disable(YZF_I2C_POINTER i2c){
    switch (i2c) {
        case HR_I2C_BASE_ADDR:
            YZF_REGV32 HR_I2C_CTRL YZF_DEL I2C_CTRL_ENABLE;
            break;
        default:
            return YZF_NOT_FOUND;
    }
    return YZF_OK;
}
YZF_RESULT yzf_i2c_dma_last_transfer(YZF_I2C_POINTER i2c){
    // i2c_dma_last_transfer_config(i2c,I2C_DMA_ON);
    return YZF_OK;
}
YZF_RESULT yzf_i2c_dma_enable(YZF_I2C_POINTER i2c){
    // i2c_dma_config(i2c,I2C_DMA_ON);
    return YZF_OK;
}
YZF_RESULT yzf_i2c_dma_disable(YZF_I2C_POINTER i2c){
    // i2c_dma_config(i2c,I2C_DMA_OFF);
    return YZF_OK;
}
YZF_RESULT yzf_i2c_buffer_interrupt_enable(YZF_I2C_POINTER i2c){
    switch (i2c) {
        case HR_I2C_BASE_ADDR:
            YZF_REGV32 HR_I2C_CTRL YZF_DEL I2C_CTRL_INT_DISABLE;
            break;
        default:
            return YZF_NOT_FOUND;
    }
    return YZF_OK;
}
YZF_RESULT yzf_i2c_buffer_interrupt_disable(YZF_I2C_POINTER i2c){
    switch (i2c) {
        case HR_I2C_BASE_ADDR:
            YZF_REGV32 HR_I2C_CTRL YZF_ADD I2C_CTRL_INT_DISABLE;
            break;
        default:
            return YZF_NOT_FOUND;
    }
    return YZF_OK;
}
YZF_RESULT yzf_i2c_event_interrupt_enable(YZF_I2C_POINTER i2c){
    switch (i2c) {
        case HR_I2C_BASE_ADDR:
            YZF_REGV32 HR_I2C_CTRL YZF_DEL I2C_CTRL_INT_DISABLE;
            break;
        default:
            return YZF_NOT_FOUND;
    }
    return YZF_OK;
}
YZF_RESULT yzf_i2c_event_interrupt_disable(YZF_I2C_POINTER i2c){
    switch (i2c) {
        case HR_I2C_BASE_ADDR:
            YZF_REGV32 HR_I2C_CTRL YZF_ADD I2C_CTRL_INT_DISABLE;
            break;
        default:
            return YZF_NOT_FOUND;
    }
    return YZF_OK;
}
YZF_RESULT yzf_i2c_address2_enable(YZF_I2C_POINTER i2c){
    return YZF_ERROR;
}
YZF_RESULT yzf_i2c_address2_disable(YZF_I2C_POINTER i2c){
    return YZF_ERROR;
}
YZF_RESULT yzf_i2c_set_address2(YZF_I2C_POINTER i2c,uint8_t address){
    return YZF_ERROR;
}
YZF_BOOL yzf_i2c_flag_timeout(YZF_I2C_POINTER i2c){
    return YZF_FALSE;
}
YZF_BOOL yzf_i2c_flag_PECERR(YZF_I2C_POINTER i2c){
    return YZF_FALSE;
}
YZF_BOOL yzf_i2c_flag_OVR(YZF_I2C_POINTER i2c){
    return YZF_FALSE;
}
YZF_BOOL yzf_i2c_flag_AF(YZF_I2C_POINTER i2c){
    return YZF_FALSE;
}
YZF_BOOL yzf_i2c_flag_ARLO(YZF_I2C_POINTER i2c){
    return YZF_FALSE;
}
YZF_BOOL yzf_i2c_flag_BERR(YZF_I2C_POINTER i2c){
    return YZF_FALSE;
}
YZF_BOOL yzf_i2c_flag_TxE(YZF_I2C_POINTER i2c){
    switch (i2c) {
        case HR_I2C_BASE_ADDR:
            return ((YZF_REGV8 HR_I2C_CR_SR) & (uint8_t) (YZF_BIT(7)|YZF_BIT(1))) == 0 ? YZF_TRUE : YZF_FALSE;
        default:
            return YZF_FALSE;
    }
}
YZF_BOOL yzf_i2c_flag_RxNE(YZF_I2C_POINTER i2c){
    switch (i2c) {
        case HR_I2C_BASE_ADDR:
            return ((YZF_REGV8 HR_I2C_CR_SR) & (uint8_t) YZF_BIT(1)) == 0 ? YZF_TRUE : YZF_FALSE;
        default:
            return YZF_FALSE;
    }
}
YZF_BOOL yzf_i2c_flag_STOPF(YZF_I2C_POINTER i2c){
    switch (i2c) {
        case HR_I2C_BASE_ADDR:
            return ((YZF_REGV8 HR_I2C_CR_SR) & (uint8_t) YZF_BIT(6)) == 7 ? YZF_TRUE : YZF_FALSE;
        default:
            return YZF_FALSE;
    }
}
YZF_BOOL yzf_i2c_flag_add10(YZF_I2C_POINTER i2c){
    return YZF_ERROR;
}
YZF_BOOL yzf_i2c_flag_BTF(YZF_I2C_POINTER i2c){
     switch (i2c) {
        case HR_I2C_BASE_ADDR:
            return ((YZF_REGV8 HR_I2C_CR_SR) & (uint8_t) YZF_BIT(1)) == 0 ? YZF_TRUE : YZF_FALSE;
        default:
            return YZF_FALSE;
    }
}
YZF_BOOL yzf_i2c_flag_ADDR(YZF_I2C_POINTER i2c){
     switch (i2c) {
        case HR_I2C_BASE_ADDR:
            return ((YZF_REGV8 HR_I2C_CR_SR) & (uint8_t)  (YZF_BIT(7)|YZF_BIT(1))) == 0 ? YZF_TRUE : YZF_FALSE;
        default:
            return YZF_FALSE;
    }
}
YZF_BOOL yzf_i2c_flag_SB(YZF_I2C_POINTER i2c){
     switch (i2c) {
        case HR_I2C_BASE_ADDR:
            // return ((YZF_REGV8 HR_I2C_CR_SR) & (uint8_t) YZF_BIT(1)) == 0 ? YZF_TRUE : YZF_FALSE;
            return YZF_TRUE;
        default:
            return YZF_FALSE;
    }
}
YZF_I2C_PEC_DATA yzf_i2c_get_pec(YZF_I2C_POINTER i2c){
    return  YZF_FALSE;
}
YZF_BOOL yzf_i2c_flag_DUALF(YZF_I2C_POINTER i2c){
    return  YZF_FALSE;
}
YZF_BOOL yzf_i2c_flag_GENCALL(YZF_I2C_POINTER i2c){
    return YZF_FALSE;
}
YZF_BOOL yzf_i2c_flag_TRA(YZF_I2C_POINTER i2c){
    return YZF_FALSE;
}
YZF_BOOL yzf_i2c_flag_BUSY(YZF_I2C_POINTER i2c){
    switch (i2c) {
        case HR_I2C_BASE_ADDR:
            return ((YZF_REGV8 HR_I2C_CR_SR) & (uint8_t) YZF_BIT(6)) == 0 ? YZF_FALSE : YZF_TRUE;
        default:
            return YZF_TRUE;
    }
}
YZF_BOOL yzf_i2c_flag_MSL(YZF_I2C_POINTER i2c){
    return YZF_FALSE;
}
YZF_RESULT yzf_i2c_set_high_mode(YZF_I2C_POINTER i2c){
    return yzf_i2c_source_clock_freq(i2c,400*1000);
}
YZF_RESULT yzf_i2c_set_normal_mode(YZF_I2C_POINTER i2c){
    return yzf_i2c_source_clock_freq(i2c,100*1000);
}
YZF_RESULT yzf_i2c_duty_cycle_2(YZF_I2C_POINTER i2c){
    return YZF_ERROR;
}
YZF_RESULT yzf_i2c_duty_cycle_16_9(YZF_I2C_POINTER i2c){
    return YZF_ERROR;
}
YZF_RESULT yzf_i2c_set_CCR(YZF_I2C_POINTER i2c,uint16_t ccr){
    return YZF_ERROR;
}
YZF_RESULT yzf_i2c_trise(YZF_I2C_POINTER i2c,uint16_t trise){
    return YZF_ERROR;
}